package week_5.homework;

import week_1.practice.EmptyCollectionException;
import week_4.practice.ElementNotFoundException;

public class ProductList<T> {
    protected int count;
    protected LinearNode head;
    protected LinearNode tail;

    public ProductList() {
        count = 0;
        head = tail = null;
    }
    public void addToFront(Product element) {
        LinearNode node = new LinearNode(element);
        if (head == null) {
            head = node;
            tail = node;
        } else {
            node.setNext(head);
            head = node;
        }
        count++;
    }

    public void addToRear(Product element) {
        LinearNode node = new LinearNode(element);

        if (isEmpty()) {
            head = node;
        } else {
            tail.setNext(node);
        }
        tail = node;
        count++;
    }

    public Product removeFirst() throws EmptyCollectionException {
        if (isEmpty()) {
            throw new EmptyCollectionException("LinkedList");
        }

        LinearNode node = head;
        Product result = node.getElement();
        head = head.getNext();

        count--;
        return result;
    }


    public Product removeLast() throws EmptyCollectionException {
        if (isEmpty()) {
            throw new EmptyCollectionException("LinkedList");
        }

        LinearNode node = head;

        Product result = node.getElement();
        while (node.getNext() != tail) {
            node = node.getNext();
        }
        tail = node;

        count--;
        return result;
    }

    public Product remove(T targetElement) throws EmptyCollectionException,
            ElementNotFoundException {
        if (isEmpty())
            throw new EmptyCollectionException("LinkedList");

        boolean found = false;
        LinearNode previous = null;
        LinearNode current = head;

        while (current != null && !found)
            if (targetElement.equals(current.getElement()))
                found = true;
            else {
                previous = current;
                current = current.getNext();
            }

        if (!found)
            throw new ElementNotFoundException("LinkedList");

        if (size() == 1)  // only one element in the list
            head = tail = null;
        else if (current.equals(head))  // target is at the head
            head = current.getNext();
        else if (current.equals(tail))  // target is at the tail
        {
            tail = previous;
            tail.setNext(null);
        } else  // target is in the middle
            previous.setNext(current.getNext());

        count--;

        return current.getElement();
    }


    public Product first() throws EmptyCollectionException {
        if (isEmpty())
            throw new EmptyCollectionException("LinkedList");

        return head.getElement();
    }


    public Product last() throws EmptyCollectionException {
        if (isEmpty()) {
            throw new EmptyCollectionException("LinkedList");
        }

        LinearNode temp = head;
        while (temp.getNext() != null) {
            temp = temp.getNext();
        }
        tail = temp;
        return tail.getElement();
    }

    public boolean contains(T targetElement) throws
            EmptyCollectionException {
        if (isEmpty())
            throw new EmptyCollectionException("LinkedList");

        boolean search = false;
        LinearNode current = new LinearNode();
        while (current.getNext() != null) {
            current = current.getNext();
        }
        if (current == targetElement)
            return search;
        else
            return !search;

    }
    public boolean isEmpty()
    {
        if(size() ==0 )
        {
            return true;
        }
        else
            return false;
    }

    public int size()
    {
        return count;
    }

    public String toString()
    {
        String result="";
        LinearNode temp = head;
        while(temp!=null)
        {
            result += temp.getElement()+"\n";
            temp = temp.getNext();
        }
        return result;
    }

    public boolean find(String tradename) {

        boolean found = false;
        LinearNode current = head;
     //   Product a = current.getElement();
        while (current != null && !found) {
            if (tradename.equals(current.getElement().getTradename()))
            {
                found=true;
            }
            else
            {
                current=current.getNext();
            }
            }
        return found;
    }

    public void seletionSort()
    {
        Product temp;
        LinearNode current = head;
        while(current!=null)
        {
            LinearNode node = current.getNext();
            while(node != null)
            {
                if(node.getElement().compareTo(current.getElement())<0)
                {
                    temp = node.getElement();
                    node.setElement(current.getElement());
                    current.setElement(temp);
                }
                node = node.getNext();
            }
            current=current.getNext();
        }
    }
}
